package com.egao.orderB.biz.impl;

import com.egao.common.core.damon.constant.UserConstant;
import com.egao.common.core.damon.results.BaseDao;
import com.egao.common.core.damon.results.PageResult2;
import com.egao.common.core.damon.utils.PinyinUtils;
import com.egao.common.core.damon.utils.Utils;
import com.egao.common.core.web.JsonResult;
import com.egao.common.system.entity.User;
import com.egao.orderB.biz.BillFeeTotalBiz;
import com.egao.orderB.entity.BillFeeTotal;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * 数据层实现类
 * @author : Doman
 * @date : 2020-12-10 15:11:18
 * @version ：1.0
 */
@Repository
public class BillFeeTotalBizImpl extends BaseDao implements BillFeeTotalBiz {

	@Override
	public JsonResult list(PageResult2<T> page, BillFeeTotal billFeeTotal, User loginUser) throws Exception {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<BillFeeTotal> rm = new BeanPropertyRowMapper<BillFeeTotal>(BillFeeTotal.class);
		StringBuffer sql = new StringBuffer("select b.id,b.organization_id,b.name");
        sql.append(",b.pinyin,b.remark,b.create_time,b.create_user");
		sql.append(",b.update_time,b.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.username createName ");
        sql.append(" from chf_bill_fee_total b ");
        sql.append(" inner join sys_user suser on suser.user_id=b.create_user ");
        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=b.organization_id ");
        sql.append(" where b.deleted=0 and sogzn.deleted=0 ");
		if(loginUser.getUserId() != UserConstant.ADMIN_ID){
            sql.append(" and b.organization_id=:tableOrganizationId ");
            map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
        }
    	if(Utils.isNotEmpty(billFeeTotal)){
			if(Utils.isNotEmpty(billFeeTotal.getName())){
				sql.append(" and (instr(b.name,:name)>0 or instr(b.pinyin,:name)>0) ");
				map.put("name", billFeeTotal.getName());
			}
			if(Utils.isNotEmpty(billFeeTotal.getSort())&&Utils.isNotEmpty(billFeeTotal.getOrder())){
                sql.append(" order by b."+billFeeTotal.getSort()+" "+billFeeTotal.getOrder());
            } else {
                sql.append(" order by b.create_time desc");
            }
		}
		return jsonResult(getPageQueryList(page, sql.toString(), map, rm));
	}

	@Override
	public List<BillFeeTotal> list(BillFeeTotal billFeeTotal, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<BillFeeTotal> rm = new BeanPropertyRowMapper<BillFeeTotal>(BillFeeTotal.class);
        StringBuffer sql = new StringBuffer("select b.id,b.organization_id,b.name");
        sql.append(",b.pinyin,b.remark,b.create_time,b.create_user");
		sql.append(",b.update_time,b.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.username createName ");
        sql.append(" from chf_bill_fee_total b ");
        sql.append(" inner join sys_user suser on suser.user_id=b.create_user ");
        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=b.organization_id ");
        sql.append(" where b.deleted=0 and sogzn.deleted=0 ");
		if(loginUser.getUserId() != UserConstant.ADMIN_ID){
            sql.append(" and b.organization_id=:tableOrganizationId ");
            map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
        }
    	if(Utils.isNotEmpty(billFeeTotal)){
			if(Utils.isNotEmpty(billFeeTotal.getName())){
				sql.append(" and (instr(b.name,:name)>0 or instr(b.pinyin,:name)>0) ");
				map.put("name", billFeeTotal.getName());
			}
            if(Utils.isNotEmpty(billFeeTotal.getSort())&&Utils.isNotEmpty(billFeeTotal.getOrder())){
				sql.append(" order by b."+billFeeTotal.getSort()+" "+billFeeTotal.getOrder());
			} else {
				sql.append(" order by b.create_time desc");
			}
		}
		getMapParameter(sql, map);
		return jdbcTemplate.query(sql.toString(),map, rm);
	}

	@Override
	public BillFeeTotal getById(BillFeeTotal billFeeTotal) {
		RowMapper<BillFeeTotal> rm = new BeanPropertyRowMapper<BillFeeTotal>(BillFeeTotal.class);
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql = new StringBuffer("select b.id,b.organization_id,b.name");
        sql.append(",b.pinyin,b.remark,b.create_time,b.create_user");
		sql.append(",b.update_time,b.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.username createName ");
        sql.append(" from chf_bill_fee_total b ");
        sql.append(" inner join sys_user suser on suser.user_id=b.create_user ");
        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=b.organization_id ");
        sql.append(" where b.deleted=0 and sogzn.deleted=0 and b.id=:id ");
		map.put("id", billFeeTotal.getId());
		getMapParameter(sql, map);
		return jdbcTemplate.queryForObject(sql.toString(), map, rm);
	}

	@Override
	public boolean save(BillFeeTotal billFeeTotal, User loginUser) {
		billFeeTotal.setPinyin(PinyinUtils.ToFirstChar(billFeeTotal.getName()));
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("insert into chf_bill_fee_total (");
        sql.append("organization_id,name,pinyin,remark");
		sql.append(",create_user,create_time) values ( ");
        sql.append(":organization_id,:name,:pinyin,:remark");
		sql.append(",:create_user,now())");
        map.put("organization_id", loginUser.getOrganizationId() + "");
        map.put("name", billFeeTotal.getName());
        map.put("pinyin", billFeeTotal.getPinyin());
        map.put("remark", billFeeTotal.getRemark());
		map.put("create_user", loginUser.getUserId() + "");
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean update(BillFeeTotal billFeeTotal, User loginUser) {
		billFeeTotal.setPinyin(PinyinUtils.ToFirstChar(billFeeTotal.getName()));
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("update chf_bill_fee_total set ");
        sql.append("name=:name,pinyin=:pinyin,remark=:remark ");
		sql.append(",update_time=now(),update_user=:update_user where id=:id ");
        map.put("name", billFeeTotal.getName());
        map.put("pinyin", billFeeTotal.getPinyin());
        map.put("remark", billFeeTotal.getRemark());
		map.put("update_user", loginUser.getUserId() + "");
		map.put("id", billFeeTotal.getId());
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean remove(BillFeeTotal billFeeTotal, User loginUser) {
		int count = 0;
		Map<String, String> map = null;
		if(billFeeTotal.getId().length()>0){
			for(String ID : billFeeTotal.getId().split(",")){
				map  = new HashMap<String, String>();
				if(ID==null || ID.equals("")){ break; }
				StringBuffer sql =new StringBuffer("update chf_bill_fee_total set deleted=1" +
						",update_user=:update_user, update_time=now() "
						+ " where id =:id ");
				map.put("update_user", loginUser.getUserId() + "");
				map.put("id", ID);
				getMapParameter(sql, map);
				count = jdbcTemplate.update(sql.toString(),map);
			}
		}
		return count > 0;
	}
}
